<?php
/**
 * reviews.php
 *
 * Tour类
 * @author konakona
 * @date 2009-10-20
 */
class reviews {
	/**
	 * 添加reviews
	 * @param string $_post int $id
	*/
	public static function reviews_add($_post){
		if($_SESSION['kona_admin']!='konakona')
			if($_SESSION['OT_admin']=='') 
				k::message('抱歉，你及不是管理员也不是操作员，无法添加你写的反馈，请联系技术员','javascript:history.go(-1);');
		if($_SESSION['kona_admin']=='konakona'){
			$name = 'konakona';
		}else{
			$name = $_SESSION['OT_admin'];
		}$pic = flight::movieFile('pic',$_FILES);
		$sql = "INSERT INTO tour_reviews values('','{$_post[reviews_name]}','{$_post[reviews_content]}',".time().",'{$pic}','{$name}',$_post[tid])";
		mysql::connect()->query($sql);
		k::message('反馈添加成功','javascript:history.go(-1);');
	}
	/**
	 * 删除reviews
	 * @param mixed $id
	*/
	public static function reviews_del($id){
		$ImageDir = "pic/";
		$pic = mysql::connect()->fetch_all("select reviews_pic from tour_reviews WHERE rid=$id");
		foreach($pic[0] as $key =>$value){
			$value = explode("|",$value);
			if($value[0]!=''){
				foreach ($value as $v){
					@unlink($ImageDir.$v);//linux主机存在权限问题，请将目录改为777权限
				}
			}
		}
		$sql = "DELETE FROM tour_reviews WHERE rid=$id";
		mysql::connect()->query($sql);
		k::message('这条reviews已删除成功','javascript:history.go(-1);');
	}
	/**
	 * 删除单张照片
	 * @param int $pic 
	 */
	public static function reviews_pic_del($pic){
		//查出ID
		$result = mysql::connect()->fetch_all("select rid,reviews_pic from tour_reviews where reviews_pic like '%{$pic}%'");
		$new_pic = str_replace($pic, "", $result[0]['reviews_pic']);
		//删除图片
		$ImageDir = "pic/".$pic;//要删除的pic路径
		$u = @unlink($ImageDir);
		if(!$u){
			template::assign('id',$result[0]['rid']);
			template::assign('reviews_details',reviews::reviews_details($result[0]['rid']));
			template::assign('errmsg','图片已经删除或找不到，请重复勿刷新本页.');
			template::display('../admin/reviews-edit.html');
		}else{
			//修改数据
			mysql::connect()->query("UPDATE `tour_reviews` SET `reviews_pic` = '{$new_pic}'  where rid = {$result[0][rid]}");
			template::assign('errmsg','图片已经删除！您可以上传新的反馈图片了！');
			template::assign('id',$result[0]['rid']);
			template::assign('reviews_details',reviews::reviews_details($result[0]['rid']));
			template::display('../admin/reviews-edit.html');
		}
	}
	/**
	 * 列出所有reviews
	 * @param int $pid $tid int $row
	 * @return array
	*/
	public static function reviews_list($pid,$tid=false,$row=10){
		if($pid == 0){
			$pid = 1;
		}
		$allcount = page::tableCount('tour_reviews');
		if($tid==false){
			if($pid==1){
				$sql = "select * from tour_reviews,tour where tour_reviews.tid=tour.id ORDER BY rid DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from tour_reviews,tour where tour_reviews.tid=tour.id ORDER BY id DESC limit ".($page_start-1).",$row";
			}
		}else{
			if($pid == 1){
				$sql = "select * from tour_reviews,tour where tour_reviews.tid = $tid and tour_reviews.tid=tour.id order by tour_reviews.rid DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from tour_reviews,tour where tour_reviews.tid = $tid and tour_reviews.tid=tour.id order by tour_reviews.rid DESC limit ".($page_start-1).",$row";
			}
		}
		$result = mysql::connect()->fetch_all($sql);
		foreach($result as $key =>$value){
			$value = explode("|",$value['reviews_pic']);
			$result[$key]['reviews_pic'] = $value; 
		}
		return $result;
	}
	/**
	 * 读取一条reviews信息
	 * @param int $id
	 * @return array
	*/
	public static function reviews_details($id){
		$sql = "select * from tour_reviews where rid=$id";
		$result = mysql::connect()->fetch_all($sql);
		$result[0]['reviews_pic'] =  explode("|",$result[0]['reviews_pic']);
		return $result[0];
	}
	/**
	 * 将数据库里的旺季和其他季月份组成select返回
	 *
	 * @param array $data
	 */
	public static function season_select($data){
		$html = '';
		foreach($data as $key=>$value){
			$data[$value] = $value;
			unset($data[$key]);
		}
		for($i=1;$i<13;$i++){
			if($i==$data[$i]){
				$html .= "<input name='high_seasons[]' type='checkbox' checked value='{$i}'>".($i)."月";
			}else{
				$html .= "<input name='high_seasons[]' type='checkbox' value='{$i}'>".($i)."月";
			}
		}
		return $html;
	}
	/**
	 * 修改一条reviews信息
	 * @param int $_post
	*/
	public static function reviews_edit(){
		$result = mysql::connect()->fetch_all("select reviews_pic from tour_reviews where rid = {$_POST[rid]}");
		$pic = !empty($_FILES['pic']['name'][0])?flight::movieFile('pic',$_FILES):'';
		$pic = $result[0]['reviews_pic'].$pic;
		$sql = "UPDATE `tour_reviews` SET `reviews_name` = '{$_POST[reviews_name]}',`reviews_content` = '{$_POST[reviews_content]}',`reviews_addtime` = ".time().",`reviews_pic` = '{$pic}' where rid={$_POST[rid]}";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=reviews-list');
	}
}